//
//  UIImage+FileName.m
//  iOS-Categories (https://github.com/shaojiankui/iOS-Categories)
//
//  Created by Jakey on 14/12/15.
//  Copyright (c) 2014年 www.skyfox.org. All rights reserved.
//
#import "UIImage+FileName.h"
#import <Foundation/Foundation.h>

@implementation UIImage (FileName)

+ (UIImage *)imageWithFileName:(NSString*)name type:(AFFImageType)type{
    if([NSString isNilOrEmpty:name])
        return nil;
    
    UIImage *img = [UIImage imageNamed:name];
    if(img)
        return img;
    
    NSString *imgPath = [self imagePathWithName:name type:type];
    if(![NSString isNilOrEmpty:imgPath]){
        img = [UIImage imageWithContentsOfFile:imgPath];
    }
    
    return img;
}

+ (NSString*)imagePathWithName:(NSString*)name type:(AFFImageType)type {
    if([NSString isNilOrEmpty:name])
        return nil;
    
    NSString *suffix = [self getSuffixWithType:type];
    
    NSString *imgPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@@%dx",name,ScreenScale>2?3:2] ofType:suffix];
    
    // 2倍优先
    if([NSString isNilOrEmpty:imgPath]){
        imgPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@@2x",name] ofType:suffix];
    }
    
    if([NSString isNilOrEmpty:imgPath]){
        imgPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@@3x",name] ofType:suffix];
    }
    
    if([NSString isNilOrEmpty:imgPath]){
        imgPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",name] ofType:suffix];
    }
    
    return imgPath;
}

+ (UIImage *)imageWithFileName:(NSString *)name {
    return [self imageWithFileName:name type:EImageType_PNG];
}

// 注意bundle下一定对应了Images目录
+ (UIImage*)imageWithBundleName:(NSString*)bundle imgName:(NSString*)imgName categ:(NSString*)categ ext:(NSString*)ext{
    NSString *bundlePath = [[ NSBundle mainBundle] pathForResource:bundle ofType:@"bundle"];
    NSString *imgPath = [NSString stringWithFormat:@"%@/%@/%@%@.png", bundlePath, categ, imgName, ext];
    UIImage *img = [UIImage imageWithContentsOfFile:imgPath];
    return img;
}

+ (UIImage *)imageWithFileNameFullScreen:(NSString *)name {
    NSString *ext = nil;
    if(iPhone6){
        ext = @"x@3x";
    }else {
        ext = ScreenScale>2?@"@3x":@"@2x";
    }
    
    return [self imageWithBundleName:@"FullScreen" imgName:name categ:@"Images" ext:ext];
}

+ (UIImage *)imageWithFileNameIssuer:(NSString *)name {
    NSString *ext = nil;
    UIImage *img = nil;
    if(iPhone6){
        ext = @"x@3x";
        img = [self imageWithBundleName:@"Issuer" imgName:name categ:kSetting.imgCagetory ext:ext];
    }
    
    if(img==nil){
        ext = ScreenScale>2?@"@3x":@"@2x";
        img = [self imageWithBundleName:@"Issuer" imgName:name categ:kSetting.imgCagetory ext:ext];
    }
    
    if(img==nil){
        ext = ScreenScale>2?@"@2x":@"";
        img = [self imageWithBundleName:@"Issuer" imgName:name categ:kSetting.imgCagetory ext:ext];
        
        if(img==nil){
            img = [self imageWithBundleName:@"Issuer" imgName:name categ:kSetting.imgCagetory ext:@""];
        }
    }
    
    // 如果还不存在调用百鱼默认
    if(img==nil && ![kSetting.imgCagetory isEqualToString:@"Images"]){
        NSString *category = kSetting.imgCagetory;
        kSetting.imgCagetory = @"Images";
        img = [self imageWithFileNameIssuer:name];
        kSetting.imgCagetory = category;
    }
    
    return img;
}





+ (UIImage *)imageWithFileNameOutBundle:(NSString*)fullName {
    if([NSString isNilOrEmpty:fullName]){
        return nil;
    }
    
    return [self imageWithFileNameOutBundle:fullName type:EImageType_PNG];
}

+ (BOOL)fileExistSuffix:(NSString*)fullName {
        NSArray *temp = [fullName componentsSeparatedByString:@"."];
    if(temp.count>0){
        NSString *suffix = [[temp lastObject] lowercaseString];
        
        return [suffix compare:@"png"]==NSOrderedSame ||
        [suffix compare:@"bmp"]==NSOrderedSame ||
        [suffix compare:@"gif"]==NSOrderedSame ||
        [suffix compare:@"jpg"]==NSOrderedSame ||
        [suffix compare:@"jpeg"]==NSOrderedSame;
    }
    
    return NO;
}

+ (NSString*)getSuffixWithType:(AFFImageType)iType {
    NSString *suffix = @"";
    switch (iType) {
        case EImageType_BMP:
            suffix = @"bmp";
            break;
            
        case EImageType_GIF:
            suffix = @"gif";
            break;
            
        case EImageType_JPG:
            suffix = @"jpg";
            break;
            
        case EImageType_PNG:
            suffix = @"png";
            break;
            
        case EImageType_ICO:
            suffix = @"ico";
            break;
            
        case EImageType_ICNS:
            suffix = @"icon";
            break;
            
        case EImageType_TIFF:
            suffix =@"tiff";
            break;
            
        case EImageType_WEBP:
            suffix = @"webp";
            break;
            
        default:
            suffix = @"未知格式";
            break;
    }
    return suffix;
}



+ (UIImage *)imageWithScreenShoot {
    CGSize imageSize = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT).size;
    
    if (NULL != &UIGraphicsBeginImageContextWithOptions) {
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    } else {
        UIGraphicsBeginImageContext(imageSize);
    }
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    NSArray *windows = [[UIApplication sharedApplication] windows];
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {
            CGContextSaveGState(context);
            
            CGContextTranslateCTM(context, [window center].x, [window center].y);
            
            CGContextConcatCTM(context, [window transform]);
            
            CGContextTranslateCTM(context,
                                  -[window bounds].size.width * [[window layer] anchorPoint].x,
                                  -[window bounds].size.height * [[window layer] anchorPoint].y);
            
            [[window layer] renderInContext:context];
            
            CGContextRestoreGState(context);
        }
    }
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}

@end
